package com.pi.shisan.util.spipoint;

import java.util.Arrays;

public abstract class BaseXyToSpi implements SimpleXyToSpiConverter{

    protected final byte[] result = new byte[8];

    protected void checkXy(int x, int y) {
        if (x < 0) {
            throw new RuntimeException("x can't less than 0.");
        }
        if (y < 0) {
            throw new RuntimeException("x can't less than 0.");
        }
        if (x > 7) {
            throw new RuntimeException("x can't great than 7.");
        }
        if (y > 7) {
            throw new RuntimeException("y can't great than 7.");
        }
    }

    protected int convert(int num) {
        return Math.abs(num) & 7;
    }

    //最右侧的8位是第一行
    //数组0 是第一行
    @Override
    public long toSpiPoint() {
        long point = 0L;
        for (int i = 0; i < result.length; i++) {
            point |= ((long)(result[result.length - i - 1]&0xFF)) << (i << 3);
        }
        return point;
    }

    @Override
    public byte[] toSpiByte() {
        return result;
    }

    @Override
    public void clear() {
        Arrays.fill(result, (byte) 0);
    }
}
